<template>
  <div class="customFormsHOW">
    <a-form :form="form" :labelCol="{ span: 6 }" :wrapperCol="{ span: 12 }">
      <a-row v-if="isReward && formFields">
        <template v-if="sortedKeys">
          <div v-for="key in sortedKeys" :key="key">
            <a-col :span="24">
              <XqItem
                ref="XqItem"
                :form="form"
                :dataSource="dataSource"
                :item="formFields[key]"
                :dynamicData="dynamicData"
                :editable="editable"
                @change="XqItemChange($event, formFields[key], dataSource, dynamicData)"
              />
            </a-col>
          </div>
        </template>
        <template v-else>
          <div v-for="item in formFields" :key="item.model">
            <a-col :span="24">
              <XqItem
                ref="XqItem"
                :form="form"
                :dataSource="dataSource"
                :item="item"
                :dynamicData="dynamicData"
                :editable="editable"
                @change="XqItemChange($event, item, dataSource, dynamicData)"
              />
            </a-col>
          </div>
        </template>
      </a-row>
    </a-form>
    <div style="height: 20px"></div>
    <div class="DynamicForm" v-if="!lookShow">
      <a-space>
        <a-button v-if="remove" type="danger" ghost @click="onRemove">删 除</a-button>
        <a-button @click="cancel">取 消</a-button>
        <a-button type="primary" @click="save">提 交</a-button>
      </a-space>
    </div>
  </div>
</template>
<script>
import XqFormMixin from "xqjr-plugin-form/mixins/XqFormMixin";
const FormMixin = XqFormMixin();
import moment from "moment";
export default {
  mixins: [FormMixin],
  props: {
    sortedKeys: {
      type: Array,
      default: () => null
    },
    remove: {
      type: Boolean,
      default: false
    },
    lookShow: {
      type: Boolean,
      default: false
    },
    dataSource: {
      type: Object,
      default: () => {}
    },
    editable: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: true
    },
    dynamicData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      isReward: true,
      spouseInfo: false,
      bankName: null,
      bankCard: null
    };
  },
  methods: {
    getFieldValue(key) {
      return this.form.getFieldValue(key);
    },
    setFieldsValueShow(value) {
      setTimeout(() => {
        this.form.setFieldsValue({ turnStraightDate: value });
      }, 100);
    },
    setFieldsValueShow1(key, value) {
      setTimeout(() => {
        this.form.setFieldsValue({ [key]: value });
      }, 100);
    },
    XqItemChange(val, item, dataSource, dynamicData) {
      if (item.model === "rewardPunish") {
        this.isReward = false;
        this.form.rewardType = null;
        this.$emit("itemChange", val);
        this.isReward = true;
      } else if (item.model === "idCard") {
        setTimeout(() => {
          this.form.setFieldsValue({ idCard: val.toUpperCase() });
          this.form.validateFields(["idCard"], (errors, values) => {});
        }, 10);
      } else if (item.model === "idcard") {
        setTimeout(() => {
          this.form.setFieldsValue({ idcard: val.toUpperCase() });
          this.form.validateFields(["idcard"], (errors, values) => {});
        }, 10);
      } else if (item.model === "annexSalaryCard") {
        if (val && val.length > 0) {
          const theItem = val[val.length - 1];
          if (theItem.response && theItem.response.code === 200) {
            setTimeout(() => {
              try {
                if (theItem.response.data.bankCardType && theItem.response.data.bankCardType !== 0) {
                  const values = this.form.getFieldsValue();
                  const valueKeys = Object.keys(values);
                  if (valueKeys.includes("bankName")) {
                    this.form.setFieldsValue({ bankName: theItem.response.data.bankName });
                  } else {
                    this.bankName = theItem.response.data.bankName;
                  }
                  if (valueKeys.includes("bankCard")) {
                    this.form.setFieldsValue({ bankCard: theItem.response.data.bankCardNumber.split(" ").join("") });
                  } else {
                    this.bankCard = theItem.response.data.bankCardNumber.split(" ").join("");
                  }
                }
              } catch (f_err) {
                console.log(f_err);
              }
            }, 10);
          }
        }
      }
      this.$emit("customValueChange", val, item.model);
    },
    save() {
      this.getData()
        .then((res) => {
          const resKeys = Object.keys(res);
          resKeys.forEach((element) => {
            if (res[element] === "true") {
              res[element] = true;
            }
            if (res[element] === "false") {
              res[element] = false;
            }
            if (res[element] && res[element]._isAMomentObject) {
              if (element === "birthday") {
                res[element] = moment(res[element]).format("YYYY-MM-DD") + " 00:00:00";
              } else {
                res[element] = moment(res[element]).format("YYYY-MM-DD");
              }
            }
            if (element === "photo" || element.includes("annex") || this.formFields[element].type === "upload") {
              const elePropType = Object.prototype.toString.call(res[element]);
              if (elePropType === "[object String]") {
                if (!res[element].includes("http")) {
                  res[element] = null;
                }
              } else if (elePropType === "[object Array]") {
                const strJoin = [];
                for (let i = 0; i < res[element].length; i++) {
                  if (Object.prototype.toString.call(res[element][i]) === "[object Object]" && res[element][i].url) {
                    strJoin.push(res[element][i].url);
                  } else if (Object.prototype.toString.call(res[element][i]) === "[object String]" && res[element][i].includes("http")) {
                    strJoin.push(res[element][i]);
                  }
                }
                res[element] = strJoin.join(",");
              } else {
                res[element] = null;
              }
            }
            if (["area"].includes(this.formFields[element].type)) {
              const elePropType = Object.prototype.toString.call(res[element]);
              if (elePropType === "[object Array]") {
                res[element] = res[element].join(",");
              }
            }
            if (["preJoinDep", "preJoinPosition"].includes(element)) {
              const elePropType = Object.prototype.toString.call(res[element]);
              if (elePropType === "[object Array]") {
                res[element] = res[element].join(",");
              }
            }
          });
          if (this.bankName && !resKeys.includes("bankName")) {
            res.bankName = this.bankName;
          }
          if (this.bankCard && !resKeys.includes("bankCard")) {
            res.bankCard = this.bankCard;
          }
          this.$emit("change", res);
        })
        .catch((err) => {
          this.$emit("change");
          console.log("debug log --> ", err);
        });
    },
    onRemove() {
      this.$emit("onRemove");
    },
    cancel() {
      this.$emit("update:editable", false);
      this.$emit("cancel");
    },
    onValuesChangeCustom(values) {}
  }
};
</script>
<style lang="scss">
.customFormsHOW {
  // position: relative;
  .ant-form-item-label > label {
    font-weight: 400;
    white-space: normal;
  }
  .ant-upload.ant-upload-select-picture-card {
    width: 79px;
    height: 80px;
  }
  .ant-upload-list-picture-card-container {
    width: 79px;
    height: 80px;
  }
  .ant-upload-list-picture-card .ant-upload-list-item {
    width: 79px;
    height: 80px;
  }
}
</style>
<style lang="scss" scoped>
.DynamicForm {
  position: fixed;
  z-index: 999;
  bottom: 0;
  right: 0;
  width: 580px;
  border-top: 1px solid #e9e9e9;
  padding: 15px 16px;
  background: #fff;
  text-align: center;
}
</style>
